package pers.kw.job.server.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.quartz.SchedulerException;
import org.springframework.web.bind.annotation.*;
import pers.kw.contants.Constants;
import pers.kw.entity.PageDto;
import pers.kw.exceptions.job.TaskException;
import pers.kw.job.server.dto.mgr.QueryJobReqDto;
import pers.kw.job.server.dto.mgr.SysJobDto;
import pers.kw.job.server.persist.entity.SysJobDO;
import pers.kw.job.server.service.SysJobService;
import pers.kw.job.server.util.CronUtils;
import pers.kw.job.server.util.ScheduleUtils;
import pers.kw.protocol.R;
import pers.kw.utils.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@Api(tags = "调度任务信息操作处理")
@RestController
@RequestMapping("/job/mgr/api/")
public class SysJobController {
    @Resource
    private SysJobService sysJobService;

    @ApiOperation(value = "查询定时任务列表")
    @PostMapping("list")
    public R<PageDto<SysJobDto>> list(QueryJobReqDto reqDto) {
        return R.ok(sysJobService.pageQueryJobList(reqDto));
    }

    /**
     * 导出定时任务列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, QueryJobReqDto sysJob) {
        sysJobService.export(response, sysJob);
    }

    /**
     * 获取定时任务详细信息
     */
    @GetMapping(value = "info/{bizCode}")
    public R<SysJobDto> getInfo(@PathVariable("bizCode") String jobId) {
        return R.ok(sysJobService.selectJobById(jobId));
    }

    /**
     * 新增定时任务
     *
     * @return
     */
    @PostMapping(value = "add")
    public R add(@RequestBody SysJobDto job) throws SchedulerException, TaskException {
        if (!CronUtils.isValid(job.getCronExpression())) {
            return R.fail("新增任务'" + job.getJobName() + "'失败，Cron表达式不正确");
        } else if (StringUtils.containsIgnoreCase(job.getInvokeTarget(), Constants.LOOKUP_RMI)) {
            return R.fail("新增任务'" + job.getJobName() + "'失败，目标字符串不允许'rmi'调用");
        } else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), new String[]{Constants.LOOKUP_LDAP, Constants.LOOKUP_LDAPS})) {
            return R.fail("新增任务'" + job.getJobName() + "'失败，目标字符串不允许'ldap(s)'调用");
        } else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), new String[]{Constants.HTTP, Constants.HTTPS})) {
            return R.fail("新增任务'" + job.getJobName() + "'失败，目标字符串不允许'http(s)'调用");
        } else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), Constants.JOB_ERROR_STR)) {
            return R.fail("新增任务'" + job.getJobName() + "'失败，目标字符串存在违规");
        } else if (ScheduleUtils.isWhiteList(job.getInvokeTarget())) {
            return R.fail("新增任务'" + job.getJobName() + "'失败，目标字符串不在白名单内");
        }
        sysJobService.insertJob(job);
        return R.ok();
    }

    /**
     * 修改定时任务
     */
    @PostMapping(value = "modify")
    public R edit(@RequestBody SysJobDto job) throws SchedulerException, TaskException {
        if (!CronUtils.isValid(job.getCronExpression())) {
            return R.fail("新增任务'" + job.getJobName() + "'失败，Cron表达式不正确");
        } else if (StringUtils.containsIgnoreCase(job.getInvokeTarget(), Constants.LOOKUP_RMI)) {
            return R.fail("新增任务'" + job.getJobName() + "'失败，目标字符串不允许'rmi'调用");
        } else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), new String[]{Constants.LOOKUP_LDAP, Constants.LOOKUP_LDAPS})) {
            return R.fail("新增任务'" + job.getJobName() + "'失败，目标字符串不允许'ldap(s)'调用");
        } else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), new String[]{Constants.HTTP, Constants.HTTPS})) {
            return R.fail("新增任务'" + job.getJobName() + "'失败，目标字符串不允许'http(s)'调用");
        } else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), Constants.JOB_ERROR_STR)) {
            return R.fail("新增任务'" + job.getJobName() + "'失败，目标字符串存在违规");
        } else if (ScheduleUtils.isWhiteList(job.getInvokeTarget())) {
            return R.fail("新增任务'" + job.getJobName() + "'失败，目标字符串不在白名单内");
        }
        sysJobService.updateJob(job);
        return R.ok();
    }

    /**
     * 定时任务状态修改
     */
    @PutMapping("/changeStatus")
    public R changeStatus(@RequestBody SysJobDto job) throws SchedulerException {
        return R.ok(sysJobService.changeStatus(job));
    }

    /**
     * 定时任务立即执行一次
     */
    @PutMapping("/run")
    public R run(@RequestBody SysJobDto job) throws SchedulerException {
        boolean result = sysJobService.run(job);
        return result ? R.ok() : R.fail("任务不存在或已过期");
    }

    /**
     * 删除定时任务
     */
    @PostMapping("del")
    public R remove(@RequestBody String[] jobBizCodes) throws SchedulerException, TaskException {
        sysJobService.deleteJobByBizCode(jobBizCodes);
        return R.ok();
    }
}
